<?php
namespace app\manage\controller;

use Baidu\Baidu;
use think\Controller;
use think\Request;
use think\Validate;
use think\Db;
use Config;
use app\common\model\TokenCheck;
use Template\Template;
use WeixinPay\Refund;
use WeixinPay\WeixinPay;

/**单元楼设置
 * Class UnitBuilding
 * @package app\manage\controller
 */
class UnitBuilding extends Controller{
	/*public function __construct()
	{
		$this->token_check = new TokenCheck();
	}*/
    /**
     * 单元楼显示
     * @return \think\response\Json
     */
    public function UnitList(){
        $model=new \app\manage\model\UnitBuilding();
        $date=$model->unitIndex();
        returnJson(200,"成功",$date)->send();die;
    }
    /**
     * 单元楼删除
     * @return \think\response\Json
     */
    public function UnitDel(){
        $model=new \app\manage\model\UnitBuilding();
        $input = input();
        $u_id = trim( $input['unit_id'] );
        $date=$model->UnitDel($u_id);
        if($date){
            returnJson(200,"成功")->send();die;
        }else{
            returnJson(-411,"单元楼下面有房源")->send();die;
        }

    }


    /**
     * 单元楼添加
     * @return \think\response\Json
     */
	public function UnitAdd(){
        $model=new \app\manage\model\UnitBuilding();
        //get的时候显示后台设置的栋数和单元楼post的时候添加
        if(Request::instance()->isGet()){
            $data=$model->unitList();
            //判断后台有没有设置单元楼栋数
            if($data==2){
                returnJson(-411,"请先在公寓设置里面设置单元楼栋数")->send();die;
            }elseif($data==3){
                returnJson(-411,"请先在公寓设置里面设置单元楼")->send();die;
            }elseif($data==4){
                returnJson(-411,"请先在公寓设置里面设置栋数")->send();die;
            }else{
                returnJson(200,"成功",$data)->send();die;
            }
        }else{
            $data = input();
            $validate = new Validate([
                ['unit_name', 'require|max:10', '请填写单元楼名称|单元楼名称不能超过10位'],
                ['layer_num', 'require|number', '请填写层数|单元楼层数必须是数字'],
                ['area', 'require|number', '请填写面积|面积必须是数字'],
                ['floor_type', 'require', '房层类型不能为空']
            ]);
            if(!$validate->check($data))
            {
                returnJson(-1001,  $validate->getError())->send();die;
            }
            $date=$model->unitAdd($data);
            if($date){
                returnJson(200,"添加成功")->send();die;
            }else{
                returnJson(-412,"添加失败单元楼重复")->send();die;
            }
        }
	}

    /**
     * 单元楼修改
     * @return \think\response\Json
     */
    public function UnitUpdate(){
        $model=new \app\manage\model\UnitBuilding();
        //get的时候显示需要修改的单元楼和单元楼post的时候修改
        if(Request::instance()->isGet()){
            $input = input();
            $u_id = trim( $input['unit_id'] );
            $data=$model->unitOne($u_id);
            //判断后台有没有设置单元楼栋数
            if($data==null){
                return json(["code"=>-413,"msg"=>'请选择正确的单元楼']);
            }else{
                return json(["code"=>200,"data"=>$data]);
            }
        }else{
            $data = input();
            $validate = new Validate([
                ['unit_name', 'require|max:10', '请填写单元楼名称|单元楼名称不能超过10位'],
                ['layer_num', 'require|number', '请填写层数|单元楼层数必须是数字'],
                ['area', 'require|number', '请填写面积|单元楼层数必须是数字'],
                ['floor_type', 'require', '房层类型不能为空']
            ]);
            if(!$validate->check($data))
            {
                returnJson(-1001,  $validate->getError())->send();die;
            }
            $date=$model->unitUpdate($data);
            if($date){
                returnJson(200,"修改成功")->send();die;
            }else{
                returnJson(-414,"修改失败")->send();die;
            }
        }
    }
//小程序模板信息发送测试
    public function cs1(){
        $input = input();
        $formId=$input['formId'];
        $appid =config('applet')['tenant']['appid'];//小程序appid
        $secret = config('applet')['tenant']['secret'];//小程序secret
        $openid = 'oZ3G-4uTCYsmFv62GUQ-M-JtBvrU';//接收用户的openid
        $template_id  = 'KpE0vtYGZt0GectuC9T80_koBcXZs1Ywu04v40IyUdo';//从微信后台获取的模板id
        $form_id = $formId;//七天内的formid
        $template=new Template();
        $data = $template->send($appid,$secret,$openid,$template_id,$form_id);
        returnJson(200,$data)->send();die;
    }
//公众号模板消息发送
    public function cs(){
// 微信接口类
      $WeChat = new  \Template\Wechat();
       $code = $_GET['code'];
      $weiwei_token = $WeChat->getToken(); // 获取微信token
      $user_info = $WeChat->getUserInfo($code, $weiwei_token);
      $openid = $user_info['openid'];

      // 获取微信用户信息
    //  $openid = 'o3On5ww9qgpQ9r9J0bYyMVa8CvhI';  //me
      # 公众号消息推送
      $i=$WeChat::pushMessage([
         // "url"=>"http://weixin.qq.com/download",
          'miniprogram'=> [
              'appid' =>'wx86ab83e50e851d0c',
              'pagepath' =>'pages/index/index',
          ],
          'openid' => $openid, // 用户openid
          'access_token' => $weiwei_token,
          'template_id' => "_FyloI1PkkqKE-nMNz67PzM5R9deXEdDsIl7i--LxfM", // 填写你自己的消息模板ID
          'data' => [ // 模板消息内容，根据模板详情进行设置
              'first'    => ['value' => urlencode("小区停电通知，您好，很抱歉的告诉您小区电梯将停止服务一段时间。"),'color' => "#743A3A"],
              'time' => ['value' => urlencode("2013年10月6日8：00-2013年10月6日18:00"),'color'=>'blue'],
              'location' => ['value' => urlencode("全小区"),'color'=>'blue'],
              'reason' => ['value' => urlencode("设备检修"),'color' => 'blue'],
              'remark'   => ['value' => urlencode("更多贷款详情，请点击页面进行实时查询。"),'color' => '#743A3A']
          ],
         // 'url_link' => 'http://weixin.qq.com/download' // 消息跳转链接
      ]);
      $data['openid']=$openid;
      $data['icode']=$i;
      returnJson(200,$data)->send();die;
  }

    /**
     * 微信小程序支付
     */
    public function pay(){
        $notify_url = 'https://apiwisdom.yuyankeji.cn/manage/Lease/orderHouse';      // 微信服务器异步通知
    //    $openid = 'oZ3G-4uTCYsmFv62GUQ-M-JtBvrU';       // 用户openid
        $openid = 'oZ3G-4peHvz-syM3YHUmQunNMhMk';       // 用户openid
        $sn=rand(1,12);
        $out_trade_no =$sn;                        // 订单编号
        $body ='你猜是什么订单';                        // 订单描述
        $total_fee = 0.01;                         // 支付金额
        // 实例微信支付基类
        $weixinPay = new WeixinPay($openid, $out_trade_no, $body, $total_fee,$notify_url);
        // 发起微信支付
        $result = $weixinPay->pay();
        returnJson(200,$result)->send();die;

    }
    /**
     * 微信退款
     */
    public function noPay(){
        $input = input();
        /*$pay_sn ='1568707386160574rbuk2sbmhl';                        // 订单编号
        $total_fee = 54.74;                         // 支付金额
        $refund_fee = 54.74;                         // 支付金额*/

        $pay_sn =$input['sn'];                        // 订单编号
        $total_fee = $input['money'];                         // 支付金额
        $refund_fee = $input['money'];                  // 支付金额*/

        // 实例微信支付基类
        $weixinPay = new WeixinPay();
        // 发起微信退款
        $result = $weixinPay->rkAlipay($pay_sn,$total_fee,$refund_fee);

        returnJson(200,$result)->send();die;

    }


    /**
     * 身份证图片识别测试
     */
    public function card(){
        $card=new Baidu();
        $img='https://apiwisdom.yuyankeji.cn/uploads/20190902/156741732731.jpg';//图片信息
        $card_side='front';//front:正面  back：反面
        $i=$card->card($img,$card_side);
        var_dump($i);exit;
    }
}